<template>
	<div class="addEdit-block" :style='{"padding":"30px"}'>
		<el-form
			:style='{"padding":"40px 30px","boxShadow":"0 1px 6px rgba(64, 158, 255, .3)","borderRadius":"6px","flexWrap":"wrap","background":"#fff","display":"flex"}'
			class="add-update-preview"
			ref="ruleForm"
			:model="ruleForm"
			:rules="rules"
			label-width="180px"
		>
			<template >
				<el-form-item :style='{"width":"100%","margin":"0 0 20px 0"}' class="input" v-if="type!='info'"  label="课程名称" prop="kechengmingcheng">
					<el-input v-model="ruleForm.kechengmingcheng" placeholder="课程名称" clearable  :readonly="ro.kechengmingcheng"></el-input>
				</el-form-item>
				<el-form-item :style='{"width":"100%","margin":"0 0 20px 0"}' v-else class="input" label="课程名称" prop="kechengmingcheng">
					<el-input v-model="ruleForm.kechengmingcheng" placeholder="课程名称" readonly></el-input>
				</el-form-item>
				<el-form-item :style='{"width":"100%","margin":"0 0 20px 0"}' class="input" v-if="type!='info'"  label="课程类型" prop="kechengleixing">
					<el-input v-model="ruleForm.kechengleixing" placeholder="课程类型" clearable  :readonly="ro.kechengleixing"></el-input>
				</el-form-item>
				<el-form-item :style='{"width":"100%","margin":"0 0 20px 0"}' v-else class="input" label="课程类型" prop="kechengleixing">
					<el-input v-model="ruleForm.kechengleixing" placeholder="课程类型" readonly></el-input>
				</el-form-item>
				<el-form-item :style='{"width":"100%","margin":"0 0 20px 0"}' class="input" v-if="type!='info'"  label="课程章节" prop="kechengzhangjie">
					<el-input v-model="ruleForm.kechengzhangjie" placeholder="课程章节" clearable  :readonly="ro.kechengzhangjie"></el-input>
				</el-form-item>
				<el-form-item :style='{"width":"100%","margin":"0 0 20px 0"}' v-else class="input" label="课程章节" prop="kechengzhangjie">
					<el-input v-model="ruleForm.kechengzhangjie" placeholder="课程章节" readonly></el-input>
				</el-form-item>
				<el-form-item :style='{"width":"100%","margin":"0 0 20px 0"}' class="upload" v-if="type!='info' && !ro.kechengtupian" label="课程图片" prop="kechengtupian">
					<file-upload
						tip="点击上传课程图片"
						action="file/upload"
						:limit="3"
						:multiple="true"
						:fileUrls="ruleForm.kechengtupian?ruleForm.kechengtupian:''"
						@change="kechengtupianUploadChange"
					></file-upload>
				</el-form-item>
				<el-form-item :style='{"width":"100%","margin":"0 0 20px 0"}' class="upload" v-else-if="ruleForm.kechengtupian" label="课程图片" prop="kechengtupian">
					<img v-if="ruleForm.kechengtupian.substring(0,4)=='http'" class="upload-img" style="margin-right:20px;" v-bind:key="index" :src="ruleForm.kechengtupian.split(',')[0]" width="100" height="100">
					<img v-else class="upload-img" style="margin-right:20px;" v-bind:key="index" v-for="(item,index) in ruleForm.kechengtupian.split(',')" :src="$base.url+item" width="100" height="100">
				</el-form-item>
				<el-form-item :style='{"width":"100%","margin":"0 0 20px 0"}' class="date" v-if="type!='info'" label="点名时间" prop="dianmingshijian">
					<el-date-picker
						value-format="yyyy-MM-dd HH:mm:ss"
						v-model="ruleForm.dianmingshijian" 
						type="datetime"
						:readonly="ro.dianmingshijian"
						placeholder="点名时间"
					></el-date-picker>
				</el-form-item>
				<el-form-item :style='{"width":"100%","margin":"0 0 20px 0"}' class="input" v-else-if="ruleForm.dianmingshijian" label="点名时间" prop="dianmingshijian">
					<el-input v-model="ruleForm.dianmingshijian" placeholder="点名时间" readonly></el-input>
				</el-form-item>
				<el-form-item :style='{"width":"100%","margin":"0 0 20px 0"}' class="input" v-if="type!='info'"  label="点名备注" prop="dianmingbeizhu">
					<el-input v-model="ruleForm.dianmingbeizhu" placeholder="点名备注" clearable  :readonly="ro.dianmingbeizhu"></el-input>
				</el-form-item>
				<el-form-item :style='{"width":"100%","margin":"0 0 20px 0"}' v-else class="input" label="点名备注" prop="dianmingbeizhu">
					<el-input v-model="ruleForm.dianmingbeizhu" placeholder="点名备注" readonly></el-input>
				</el-form-item>
				<el-form-item :style='{"width":"100%","margin":"0 0 20px 0"}' class="select" v-if="type!='info'"  label="签到状态" prop="qiandaozhuangtai">
					<el-select :disabled="ro.qiandaozhuangtai" v-model="ruleForm.qiandaozhuangtai" placeholder="请选择签到状态" >
						<el-option
							v-for="(item,index) in qiandaozhuangtaiOptions"
							v-bind:key="index"
							:label="item"
							:value="item">
						</el-option>
					</el-select>
				</el-form-item>
				<el-form-item :style='{"width":"100%","margin":"0 0 20px 0"}' v-else class="input" label="签到状态" prop="qiandaozhuangtai">
					<el-input v-model="ruleForm.qiandaozhuangtai"
						placeholder="签到状态" readonly></el-input>
				</el-form-item>
				<el-form-item :style='{"width":"100%","margin":"0 0 20px 0"}' class="select" v-if="type!='info'" label="学员学号" prop="xueyuanxuehao">
					<el-select :disabled="ro.xueyuanxuehao" @change="xueyuanxuehaoChange" v-model="ruleForm.xueyuanxuehao" placeholder="请选择学员学号">
						<el-option
							v-for="(item,index) in xueyuanxuehaoOptions"
							v-bind:key="index"
							:label="item"
							:value="item">
						</el-option>
					</el-select>
				</el-form-item>
				<el-form-item :style='{"width":"100%","margin":"0 0 20px 0"}' class="input" v-else-if="ruleForm.xueyuanxuehao" label="学员学号" prop="xueyuanxuehao">
					<el-input v-model="ruleForm.xueyuanxuehao" placeholder="学员学号" readonly></el-input>
				</el-form-item>
				<el-form-item :style='{"width":"100%","margin":"0 0 20px 0"}' class="input" v-if="type!='info'"  label="学员姓名" prop="xueyuanxingming">
					<el-input v-model="ruleForm.xueyuanxingming" placeholder="学员姓名" clearable  :readonly="ro.xueyuanxingming"></el-input>
				</el-form-item>
				<el-form-item :style='{"width":"100%","margin":"0 0 20px 0"}' v-else class="input" label="学员姓名" prop="xueyuanxingming">
					<el-input v-model="ruleForm.xueyuanxingming" placeholder="学员姓名" readonly></el-input>
				</el-form-item>
				<el-form-item :style='{"width":"100%","margin":"0 0 20px 0"}' class="input" v-if="type!='info'"  label="教辅工号" prop="jiaofugonghao">
					<el-input v-model="ruleForm.jiaofugonghao" placeholder="教辅工号" clearable  :readonly="ro.jiaofugonghao"></el-input>
				</el-form-item>
				<el-form-item :style='{"width":"100%","margin":"0 0 20px 0"}' v-else class="input" label="教辅工号" prop="jiaofugonghao">
					<el-input v-model="ruleForm.jiaofugonghao" placeholder="教辅工号" readonly></el-input>
				</el-form-item>
				<el-form-item :style='{"width":"100%","margin":"0 0 20px 0"}' class="input" v-if="type!='info'"  label="教辅姓名" prop="jiaofuxingming">
					<el-input v-model="ruleForm.jiaofuxingming" placeholder="教辅姓名" clearable  :readonly="ro.jiaofuxingming"></el-input>
				</el-form-item>
				<el-form-item :style='{"width":"100%","margin":"0 0 20px 0"}' v-else class="input" label="教辅姓名" prop="jiaofuxingming">
					<el-input v-model="ruleForm.jiaofuxingming" placeholder="教辅姓名" readonly></el-input>
				</el-form-item>
			</template>
			<el-form-item :style='{"width":"100%","padding":"0","margin":"20px 0 0"}' class="btn">
				<el-button class="btn3"  v-if="type!='info'" type="success" @click="onSubmit">
					<span class="icon iconfont " :style='{"margin":"0 2px","fontSize":"14px","color":"#fff","height":"40px"}'></span>
					提交
				</el-button>
				<el-button class="btn4" v-if="type!='info'" type="success" @click="back()">
					<span class="icon iconfont " :style='{"margin":"0 2px","fontSize":"14px","color":"#fff","height":"40px"}'></span>
					取消
				</el-button>
				<el-button class="btn5" v-if="type=='info'" type="success" @click="back()">
					<span class="icon iconfont " :style='{"margin":"0 2px","fontSize":"14px","color":"#fff","height":"40px"}'></span>
					返回
				</el-button>
			</el-form-item>
		</el-form>
    

  </div>
</template>
<script>
export default {
	data() {
		return {
			id: '',
			type: '',
			
			
			ro:{
				kechengmingcheng : false,
				kechengleixing : false,
				kechengzhangjie : false,
				kechengtupian : false,
				dianmingshijian : false,
				dianmingbeizhu : false,
				qiandaozhuangtai : false,
				xueyuanxuehao : false,
				xueyuanxingming : false,
				jiaofugonghao : false,
				jiaofuxingming : false,
			},
			
			
			ruleForm: {
				kechengmingcheng: '',
				kechengleixing: '',
				kechengzhangjie: '',
				kechengtupian: '',
				dianmingshijian: '',
				dianmingbeizhu: '',
				qiandaozhuangtai: '未签到',
				xueyuanxuehao: '',
				xueyuanxingming: '',
				jiaofugonghao: '',
				jiaofuxingming: '',
			},
		
			qiandaozhuangtaiOptions: [],
			xueyuanxuehaoOptions: [],

			
			rules: {
				kechengmingcheng: [
				],
				kechengleixing: [
				],
				kechengzhangjie: [
				],
				kechengtupian: [
				],
				dianmingshijian: [
				],
				dianmingbeizhu: [
				],
				qiandaozhuangtai: [
				],
				xueyuanxuehao: [
					{ required: true, message: '学员学号不能为空', trigger: 'blur' },
				],
				xueyuanxingming: [
				],
				jiaofugonghao: [
				],
				jiaofuxingming: [
				],
			},
		};
	},
	props: ["parent"],
	computed: {



	},
    components: {
    },
	created() {
		this.ruleForm.dianmingshijian = this.getCurDateTime()
	},
	methods: {
		
		// 下载
		download(file){
			window.open(`${file}`)
		},
		// 初始化
		init(id,type) {
			if (id) {
				this.id = id;
				this.type = type;
			}
			if(this.type=='info'||this.type=='else'){
				this.info(id);
			}else if(this.type=='logistics'){
				this.logistics=false;
				this.info(id);
			}else if(this.type=='cross'){
				var obj = this.$storage.getObj('crossObj');
				for (var o in obj){
						if(o=='kechengmingcheng'){
							this.ruleForm.kechengmingcheng = obj[o];
							this.ro.kechengmingcheng = true;
							continue;
						}
						if(o=='kechengleixing'){
							this.ruleForm.kechengleixing = obj[o];
							this.ro.kechengleixing = true;
							continue;
						}
						if(o=='kechengzhangjie'){
							this.ruleForm.kechengzhangjie = obj[o];
							this.ro.kechengzhangjie = true;
							continue;
						}
						if(o=='kechengtupian'){
							this.ruleForm.kechengtupian = obj[o];
							this.ro.kechengtupian = true;
							continue;
						}
						if(o=='dianmingshijian'){
							this.ruleForm.dianmingshijian = obj[o];
							this.ro.dianmingshijian = true;
							continue;
						}
						if(o=='dianmingbeizhu'){
							this.ruleForm.dianmingbeizhu = obj[o];
							this.ro.dianmingbeizhu = true;
							continue;
						}
						if(o=='qiandaozhuangtai'){
							this.ruleForm.qiandaozhuangtai = obj[o];
							this.ro.qiandaozhuangtai = true;
							continue;
						}
						if(o=='xueyuanxuehao'){
							this.ruleForm.xueyuanxuehao = obj[o];
							this.ro.xueyuanxuehao = true;
							continue;
						}
						if(o=='xueyuanxingming'){
							this.ruleForm.xueyuanxingming = obj[o];
							this.ro.xueyuanxingming = true;
							continue;
						}
						if(o=='jiaofugonghao'){
							this.ruleForm.jiaofugonghao = obj[o];
							this.ro.jiaofugonghao = true;
							continue;
						}
						if(o=='jiaofuxingming'){
							this.ruleForm.jiaofuxingming = obj[o];
							this.ro.jiaofuxingming = true;
							continue;
						}
				}
				this.ruleForm.qiandaozhuangtai = '未签到'; 			}
			// 获取用户信息
			this.$http({
				url: `${this.$storage.get('sessionTable')}/session`,
				method: "get"
			}).then(({ data }) => {
				if (data && data.code === 0) {
					var json = data.data;
					if(this.$storage.get("role")!="管理员") {
						this.ro.qiandaozhuangtai = true;
					}
					if(((json.jiaofugonghao!=''&&json.jiaofugonghao) || json.jiaofugonghao==0) && this.$storage.get("role")!="管理员"){
						this.ruleForm.jiaofugonghao = json.jiaofugonghao
						this.ro.jiaofugonghao = true;
					}
					if(((json.jiaofuxingming!=''&&json.jiaofuxingming) || json.jiaofuxingming==0) && this.$storage.get("role")!="管理员"){
						this.ruleForm.jiaofuxingming = json.jiaofuxingming
						this.ro.jiaofuxingming = true;
					}
				} else {
					this.$message.error(data.msg);
				}
			});
            this.qiandaozhuangtaiOptions = "已签到,未签到".split(',')
            this.$http({
				url: `option/xueyuan/xueyuanxuehao`,
				method: "get"
            }).then(({ data }) => {
				if (data && data.code === 0) {
					this.xueyuanxuehaoOptions = data.data;
				} else {
					this.$message.error(data.msg);
				}
            });
			
		},
			// 下二随
			xueyuanxuehaoChange () {
				this.$http({
					url: `follow/xueyuan/xueyuanxuehao?columnValue=`+ this.ruleForm.xueyuanxuehao,
					method: "get"
				}).then(({ data }) => {
					if (data && data.code === 0) {
						if(data.data.xueyuanxingming){
							this.ruleForm.xueyuanxingming = data.data.xueyuanxingming
						}
					} else {
						this.$message.error(data.msg);
					}
				});
			},
    // 多级联动参数

		info(id) {
			this.$http({
				url: `ketangdianming/info/${id}`,
				method: "get"
			}).then(({ data }) => {
				if (data && data.code === 0) {
					this.ruleForm = data.data;
					//解决前台上传图片后台不显示的问题
					let reg=new RegExp('../../../upload','g')//g代表全部
				} else {
					this.$message.error(data.msg);
				}
			});
		},


    // 提交
    onSubmit() {
	if(this.ruleForm.kechengtupian!=null) {
		this.ruleForm.kechengtupian = this.ruleForm.kechengtupian.replace(new RegExp(this.$base.url,"g"),"");
	}
var objcross = this.$storage.getObj('crossObj');
      //更新跨表属性
       var crossuserid;
       var crossrefid;
       var crossoptnum;
       if(this.type=='cross'){
                var statusColumnName = this.$storage.get('statusColumnName');
                var statusColumnValue = this.$storage.get('statusColumnValue');
                if(statusColumnName!='') {
                        var obj = this.$storage.getObj('crossObj');
                       if(statusColumnName && !statusColumnName.startsWith("[")) {
                               for (var o in obj){
                                 if(o==statusColumnName){
                                   obj[o] = statusColumnValue;
                                 }
                               }
                               var table = this.$storage.get('crossTable');
                             this.$http({
                                 url: `${table}/update`,
                                 method: "post",
                                 data: obj
                               }).then(({ data }) => {});
                       } else {
                               crossuserid=this.$storage.get('userid');
                               crossrefid=obj['id'];
                               crossoptnum=this.$storage.get('statusColumnName');
                               crossoptnum=crossoptnum.replace(/\[/,"").replace(/\]/,"");
                        }
                }
        }
		this.$refs["ruleForm"].validate(valid => {
			if (valid) {
				if(crossrefid && crossuserid) {
					this.ruleForm.crossuserid = crossuserid;
					this.ruleForm.crossrefid = crossrefid;
					let params = { 
						page: 1, 
						limit: 10, 
						crossuserid:this.ruleForm.crossuserid,
						crossrefid:this.ruleForm.crossrefid,
					} 
				this.$http({ 
					url: "ketangdianming/page", 
					method: "get", 
					params: params 
				}).then(({ 
					data 
				}) => { 
					if (data && data.code === 0) { 
						if(data.data.total>=crossoptnum) {
							this.$message.error(this.$storage.get('tips'));
							return false;
						} else {
							this.$http({
								url: `ketangdianming/${!this.ruleForm.id ? "save" : "update"}`,
								method: "post",
								data: this.ruleForm
							}).then(({ data }) => {
								if (data && data.code === 0) {
									this.$message({
										message: "操作成功",
										type: "success",
										duration: 1500,
										onClose: () => {
											this.parent.showFlag = true;
											this.parent.addOrUpdateFlag = false;
											this.parent.ketangdianmingCrossAddOrUpdateFlag = false;
											this.parent.search();
											this.parent.contentStyleChange();
										}
									});
								} else {
									this.$message.error(data.msg);
								}
							});

						}
					} else { 
				} 
			});
		} else {
			this.$http({
				url: `ketangdianming/${!this.ruleForm.id ? "save" : "update"}`,
				method: "post",
			   data: this.ruleForm
			}).then(({ data }) => {
				if (data && data.code === 0) {
					this.$message({
						message: "操作成功",
						type: "success",
						duration: 1500,
						onClose: () => {
							this.parent.showFlag = true;
							this.parent.addOrUpdateFlag = false;
							this.parent.ketangdianmingCrossAddOrUpdateFlag = false;
							this.parent.search();
							this.parent.contentStyleChange();
						}
					});
				} else {
					this.$message.error(data.msg);
			   }
			});
		 }
         }
		});
    },
    // 获取uuid
    getUUID () {
      return new Date().getTime();
    },
    // 返回
    back() {
      this.parent.showFlag = true;
      this.parent.addOrUpdateFlag = false;
      this.parent.ketangdianmingCrossAddOrUpdateFlag = false;
      this.parent.contentStyleChange();
    },
    kechengtupianUploadChange(fileUrls) {
	    this.ruleForm.kechengtupian = fileUrls;
    },
  }
};
</script>
<style lang="scss" scoped>
	.amap-wrapper {
		width: 100%;
		height: 500px;
	}
	
	.search-box {
		position: absolute;
	}
	
	.el-date-editor.el-input {
		width: auto;
	}
	
	.add-update-preview .el-form-item /deep/ .el-form-item__label {
	  	  padding: 0 10px 0 0;
	  	  color: #333;
	  	  font-weight: 500;
	  	  width: 180px;
	  	  font-size: 14px;
	  	  line-height: 40px;
	  	  text-align: right;
	  	}
	
	.add-update-preview .el-form-item /deep/ .el-form-item__content {
	  margin-left: 180px;
	}
	
	.add-update-preview .el-input /deep/ .el-input__inner {
	  	  border: 1px solid #000;
	  	  border-radius: 4px;
	  	  padding: 0 12px;
	  	  outline: none;
	  	  color: #000;
	  	  width: 400px;
	  	  font-size: 14px;
	  	  height: 40px;
	  	}
	.add-update-preview .el-input-number /deep/ .el-input__inner {
		text-align: left;
	  	  border: 1px solid #000;
	  	  border-radius: 4px;
	  	  padding: 0 12px;
	  	  outline: none;
	  	  color: #000;
	  	  width: 400px;
	  	  font-size: 14px;
	  	  height: 40px;
	  	}
	.add-update-preview .el-input-number /deep/ .el-input-number__decrease {
		display: none;
	}
	.add-update-preview .el-input-number /deep/ .el-input-number__increase {
		display: none;
	}
	
	.add-update-preview .el-select /deep/ .el-input__inner {
	  	  border: 1px solid #000;
	  	  border-radius: 4px;
	  	  padding: 0 10px;
	  	  outline: none;
	  	  color: #000;
	  	  width: 200px;
	  	  font-size: 14px;
	  	  height: 40px;
	  	}
	
	.add-update-preview .el-date-editor /deep/ .el-input__inner {
	  	  border: 1px solid #000;
	  	  border-radius: 4px;
	  	  padding: 0 10px 0 30px;
	  	  outline: none;
	  	  color: #000;
	  	  width: 200px;
	  	  font-size: 14px;
	  	  height: 40px;
	  	}
	
	.add-update-preview /deep/ .el-upload--picture-card {
		background: transparent;
		border: 0;
		border-radius: 0;
		width: auto;
		height: auto;
		line-height: initial;
		vertical-align: middle;
	}
	
	.add-update-preview /deep/ .upload .upload-img {
	  	  border: 1px solid #000;
	  	  cursor: pointer;
	  	  border-radius: 6px;
	  	  color: #888;
	  	  width: 100px;
	  	  font-size: 32px;
	  	  line-height: 80px;
	  	  text-align: center;
	  	  height: 80px;
	  	}
	
	.add-update-preview /deep/ .el-upload-list .el-upload-list__item {
	  	  border: 1px solid #000;
	  	  cursor: pointer;
	  	  border-radius: 6px;
	  	  color: #888;
	  	  width: 100px;
	  	  font-size: 32px;
	  	  line-height: 80px;
	  	  text-align: center;
	  	  height: 80px;
	  	}
	
	.add-update-preview /deep/ .el-upload .el-icon-plus {
	  	  border: 1px solid #000;
	  	  cursor: pointer;
	  	  border-radius: 6px;
	  	  color: #888;
	  	  width: 100px;
	  	  font-size: 32px;
	  	  line-height: 80px;
	  	  text-align: center;
	  	  height: 80px;
	  	}
	
	.add-update-preview .el-textarea /deep/ .el-textarea__inner {
	  	  border: 1px solid #000;
	  	  border-radius: 4px;
	  	  padding: 12px;
	  	  color: #000;
	  	  width: 400px;
	  	  font-size: 14px;
	  	  height: 120px;
	  	}
	
	.add-update-preview .btn .btn1 {
				border: 0;
				cursor: pointer;
				border-radius: 4px;
				padding: 0 24px;
				margin: 4px;
				outline: none;
				color: #fff;
				background: #1DD9F8;
				width: auto;
				font-size: 14px;
				height: 40px;
			}
	
	.add-update-preview .btn .btn1:hover {
				opacity: 0.8;
			}
	
	.add-update-preview .btn .btn2 {
				border: 0;
				cursor: pointer;
				border-radius: 4px;
				padding: 0 24px;
				margin: 4px;
				outline: none;
				color: #fff;
				background: #1DD9F8;
				width: auto;
				font-size: 14px;
				height: 40px;
			}
	
	.add-update-preview .btn .btn2:hover {
				opacity: 0.8;
			}
	
	.add-update-preview .btn .btn3 {
				border: 0;
				cursor: pointer;
				border-radius: 4px;
				padding: 0 24px;
				margin: 4px;
				outline: none;
				color: #fff;
				background: #1DD9F8;
				width: auto;
				font-size: 14px;
				height: 40px;
			}
	
	.add-update-preview .btn .btn3:hover {
				opacity: 0.8;
			}
	
	.add-update-preview .btn .btn4 {
				border: 0;
				cursor: pointer;
				border-radius: 4px;
				padding: 0 24px;
				margin: 4px;
				outline: none;
				color: #fff;
				background: #1DD9F8;
				width: auto;
				font-size: 14px;
				height: 40px;
			}
	
	.add-update-preview .btn .btn4:hover {
				opacity: 0.8;
			}
	
	.add-update-preview .btn .btn5 {
				border: 0;
				cursor: pointer;
				border-radius: 4px;
				padding: 0 24px;
				margin: 4px;
				outline: none;
				color: #fff;
				background: #1DD9F8;
				width: auto;
				font-size: 14px;
				height: 40px;
			}
	
	.add-update-preview .btn .btn5:hover {
				opacity: 0.8;
			}
</style>
